//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using DotNetNuke.Security.Roles;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Utilities;
namespace DotNetNuke.Modules.Admin.Security
{
	partial class EditGroups : DotNetNuke.Entities.Modules.PortalModuleBase
	{
		private int RoleGroupID = -1;
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cmdCancel.Click += new EventHandler(cmdCancel_Click);
			cmdDelete.Click += new EventHandler(cmdDelete_Click);
			cmdUpdate.Click += new EventHandler(cmdUpdate_Click);

			try {
				if ((Request.QueryString["RoleGroupID"] != null)) {
					RoleGroupID = Int32.Parse(Request.QueryString["RoleGroupID"]);
				}
				if (Page.IsPostBack == false) {
					ClientAPI.AddButtonConfirm(cmdDelete, Services.Localization.Localization.GetString("DeleteItem"));
					RoleController objRoles = new RoleController();
					if (RoleGroupID != -1) {
						RoleGroupInfo objRoleGroupInfo = RoleController.GetRoleGroup(PortalId, RoleGroupID);
						if (objRoleGroupInfo != null) {
							txtRoleGroupName.Text = objRoleGroupInfo.RoleGroupName;
							txtDescription.Text = objRoleGroupInfo.Description;
							int roleCount = objRoles.GetRolesByGroup(PortalId, RoleGroupID).Count;
							if (roleCount > 0) {
								cmdDelete.Visible = false;
							}
						} else {
							Response.Redirect(Common.Globals.NavigateURL("Security Roles"));
						}
					} else {
						cmdDelete.Visible = false;
					}
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdUpdate_Click(object sender, EventArgs e)
		{
			try {
				if (Page.IsValid) {
					RoleGroupInfo objRoleGroupInfo = new RoleGroupInfo();
					objRoleGroupInfo.PortalID = PortalId;
					objRoleGroupInfo.RoleGroupID = RoleGroupID;
					objRoleGroupInfo.RoleGroupName = txtRoleGroupName.Text;
					objRoleGroupInfo.Description = txtDescription.Text;
					if (RoleGroupID == -1) {
						try {
							RoleController.AddRoleGroup(objRoleGroupInfo);
						} catch {
							DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("DuplicateRoleGroup", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
							return;
						}
						Response.Redirect(Common.Globals.NavigateURL(TabId, ""));
					} else {
						RoleController.UpdateRoleGroup(objRoleGroupInfo);
						Response.Redirect(Common.Globals.NavigateURL(TabId, "", "RoleGroupID=" + RoleGroupID.ToString()));
					}
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdDelete_Click(object sender, System.EventArgs e)
		{
			try {
				RoleController.DeleteRoleGroup(PortalId, RoleGroupID);
				Response.Redirect(Common.Globals.NavigateURL());
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdCancel_Click(object sender, System.EventArgs e)
		{
			try {
				if (RoleGroupID == -1) {
					Response.Redirect(Common.Globals.NavigateURL(TabId, ""));
				} else {
					Response.Redirect(Common.Globals.NavigateURL(TabId, "", "RoleGroupID=" + RoleGroupID.ToString()));
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
	}
}
